{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.2 64-bit",
   "metadata": {
    "interpreter": {
     "hash": "63fd5069d213b44bf678585dea6b12cceca9941eaf7f819626cde1f2670de90d"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 泊松分布\n",
    "\n",
    "若一个随机变量$X$的概率分布为\n",
    "\n",
    "$$\n",
    "P(X=k)=e^{-\\lambda}\\frac{\\lambda^k}{k!}, \\lambda > 0, k =0,1,2,...,\n",
    "$$\n",
    "\n",
    "则称$X$服从参数为$\\lambda$的**泊松分布** (Possion Distribution), 记为 $X \\sim P(\\lambda)$ (或$X \\sim \\pi (\\lambda)$)"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "poisson = lambda k, lmd: (math.e**(-lmd))*(lmd**k)/(math.factorial(k))"
   ]
  },
  {
   "source": [
    "易见，\n",
    "\n",
    "$$\n",
    "\\text{(1) } P(X=k) \\geq 0, k = 0,1,2,...; \n",
    "$$\n",
    "\n",
    "$$\n",
    "\\text{(2) } \\sum_{k=0}^{\\infty} P(X=k) = \\sum_{k=0}^{\\infty} e^{-\\lambda} \\frac{\\lambda^k}{k!} = e^{-\\lambda} \\sum_{k=0}^{\\infty} \\frac{\\lambda^k}{k!} = e^{-\\lambda} e^{\\lambda} = 1\n",
    "$$"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "\\lambda = 5, \\sum_{k=0}^{10}P(X = k) \\approx  0.9863047314016172\n\\lambda = 5, \\sum_{k=0}^{20}P(X = k) \\approx  0.9999999189074956\n\\lambda = 5, \\sum_{k=0}^{30}P(X = k) \\approx  0.9999999999999958\n\\lambda = 5, \\sum_{k=0}^{40}P(X = k) \\approx  1.0000000000000002\n"
     ]
    }
   ],
   "source": [
    "def poisson_terms_sum(lmd, k_range):\n",
    "    the_sum = 0\n",
    "    for k in k_range:\n",
    "        the_sum += poisson(k, lmd)\n",
    "    return the_sum\n",
    "\n",
    "print(R\"\\lambda = 5, \\sum_{k=0}^{10}P(X = k) \\approx \", poisson_terms_sum(5, range(0, 10 + 1)))\n",
    "print(R\"\\lambda = 5, \\sum_{k=0}^{20}P(X = k) \\approx \", poisson_terms_sum(5, range(0, 20 + 1)))\n",
    "print(R\"\\lambda = 5, \\sum_{k=0}^{30}P(X = k) \\approx \", poisson_terms_sum(5, range(0, 30 + 1)))\n",
    "print(R\"\\lambda = 5, \\sum_{k=0}^{40}P(X = k) \\approx \", poisson_terms_sum(5, range(0, 40 + 1)))"
   ]
  },
  {
   "source": [
    "**例** 一家商店由过去的销售记录可知，某种商品某月的销售数量可以用参数$\\lambda = 5$的泊松分布来描述，为了有$95\\%$以上的把握商品不会脱销，则商店在月底至少应该进该种商品多少件？\n",
    "\n",
    "**解** 设该商品每月的销售数为 $X$, 已知 $X$ 服从参数 $\\lambda = 5$ 的泊松分布. 设商店应进货该商品 $m$ 件, 求满足 $P(X \\leq m) > 0.95$ 的最小的 $m$, 即\n",
    "\n",
    "$$\n",
    "\\sum^{m}_{k=0}\\frac{e^{-5}5^k}{k!} > 0.95\n",
    "$$"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "应进货 9 件商品。\n"
     ]
    }
   ],
   "source": [
    "m = 1\n",
    "while poisson_terms_sum(5, range(0, m + 1)) <= 0.95:\n",
    "    m = m + 1\n",
    "print(\"应进货 {} 件商品。\".format(m))"
   ]
  }
 ]
}